<template>
  <div class="admin_right_main">
    <div
      class="search_template"
      style="background:white;height:120px;"
    >
      <div
        class="content"
        style="height: 80px; display: flex; justify-content: space-around;align-items: center;">
        <div>
          <span style="color:#888; font-weight:800;">公告标题:</span>
          <el-input
            v-model="search"
            placeholder="请输入公告标题"
            @keyup.enter.native="onEnterSearch"
            style="width:200px;height50px;"
          />
        </div>
        <div align="right">
          <el-button size="mini" icon="el-icon-search" type="success" @click="onEnterSearch">点击查询</el-button>
        </div>
      </div>
      <div align="left" style="margin-left:10px">
        <el-button size="mini" type="primary" plain icon="el-icon-plus" @click="addEngineering">新增</el-button>
      </div>
    </div>
    <div class="table_template">
      <el-table
        ref="table"
        :data="tableData"
         border
        style="width: 100%"
        height="550"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width=""></el-table-column>
        <el-table-column align="center" prop="title" label="公告标题" width=""></el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间" width=""></el-table-column>
        <el-table-column align="center" prop="isShow" label="是否显示或隐藏" :formatter="formatRole" width=""></el-table-column>
        <el-table-column align="center" prop="auditStatus" label="审核状态" :formatter="formatRole_two" width=""></el-table-column>
        <el-table-column align="center" width="400" label="操作">
          <!--   <template slot="header" slot-scope="scope">  
		 				<el-button
		 				  size="mini"
		 				  @click="add_add_organization(scope)">新增</el-button>
          </template>-->
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="editEngineering">修改</el-button>
            <el-button size="mini" type="primary" plain @click="auditEngineering">审核</el-button>
            <el-button size="mini" type="primary" plain @click="releaseEngineering">发布</el-button>
            <el-button size="mini" type="danger" plain @click="deleteEngineering">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table_page">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          style="width: 100%;position: relative;display: flex;justify-content: flex-end;padding: 0 20px;"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <!--弹出框--新增-->
    <el-dialog title="新增信息" :visible.sync="dialogVisibleAdd" :destroy-on-close="true" width="55%">
      <span>
        <FileAdd ref="FileAdd" @closeModal="closeAdd"></FileAdd>
      </span>
      <!-- 操作按钮 -->
      <div slot="footer">
        <el-button size="mini" type="info" class="table_header_btn" @click="saveAdd">保存</el-button>
        <el-button
          size="mini"
          type="info"
          class="table_header_btn"
          @click="dialogVisibleAdd = false"
        >关闭</el-button>
      </div>
    </el-dialog>
    <!--弹出框--修改-->
    <el-dialog title="修改信息" :visible.sync="dialogVisibleEdit" :destroy-on-close="true" width="55%">
      <span>
        <FileModification
          ref="FileModification"
          :EngineeringData="multipleSelection"
          @closeModal="closeEdit"
        ></FileModification>
      </span>
      <div slot="footer">
        <el-button size="mini" type="info" class="table_header_btn" @click="saveEdit">保存</el-button>
        <el-button
          size="mini"
          type="info"
          class="table_header_btn"
          @click="dialogVisibleEdit = false"
        >关闭</el-button>
      </div>
    </el-dialog>
    <!--弹出框--审核-->
    <el-dialog title="审核信息" :visible.sync="dialogVisibleAudit" :destroy-on-close="true" width="55%">
      <span>
        <auditIndex
          ref="auditIndex"
          @fatherMethod="getTableData"
          :EngineeringData="multipleSelection"
          @closeModal="closeExamine"
        ></auditIndex>
      </span>
      <div slot="footer">
        <el-button size="mini" type="info" class="table_header_btn" @click="saveAudit">审核</el-button>
        <el-button size="mini" type="info" class="table_header_btn" @click="unsaveAudit">退回</el-button>
        <el-button
          size="mini"
          type="info"
          class="table_header_btn"
          @click="dialogVisibleAudit = false"
        >关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import FileAdd from './noticeAdd';
import { noticeList, deleteNotice, roleDeptTreeselect1, checkNoticeStatus, to_examine_NoticeId, noticeSearch } from '@/api/user';
import FileModification from './noticeCation';
import auditIndex from './auditIndex';
import { getToken, setToken, removeToken } from '@/utils/auth';
// 定义一个全局变量,存放table被选中的行索引
export default {
    components: {
        FileAdd,
        FileModification,
        auditIndex
    },
    computed: {},
    data() {
        return {
            tableOption: { isShowHeader: true, height: 'auto' },
            multipleSelection: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            dialogVisibleAdd: false,
            dialogVisibleEdit: false,
            dialogVisibleAudit: false,
            form: {
                title: '',
                createTime: '', //时间
                auditStatus: '',
                isShow: '',
                id: ''
            },
            search: '',
            tableData: []
        };
    },

    created() {
        this.getTableData();
    },
    methods: {
        formatRole: function(row, column) {
            return row.isShow == '0' ? '显示' : row.isShow == '1' ? '隐藏' : '隐藏';
        },
        formatRole_two: function(row, column) {
            return row.auditStatus == '0'
                ? '待审核'
                : row.auditStatus == '1'
                    ? '审核通过'
                    : row.auditStatus == '2'
                        ? '已发布'
                        : row.auditStatus == '3'
                            ? '审核被拒'
                            : '隐藏';
        },
        getTableData() {
            console.log('父组件方法');
            var noticeList_data = {
                // token: getToken(),
                pageNum: this.pageNum,
                pageSize: this.pageSize
            };
            noticeList(noticeList_data)
                .then(response => {
                    if (response.data.code == 200) {
                        console.log('=博阳==', response.data);
                        this.tableData = response.data.data.list;
                        this.total = response.data.data.total;
                        this.pageSize = response.data.data.pageSize;
                        this.pageNum = response.data.data.pageNum;
                    } else {
                        // this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {
                    // this.loading = false
                    // this.$message.error(response.data.msg)
                });
        },
        //添加的弹出框
        addEngineering() {
            this.dialogVisibleAdd = true;
        },
        //修改弹出框
        editEngineering() {
            if (this.multipleSelection.length != 1 || this.multipleSelection.length > 1) {
                this.$notify.error({
                    title: '错误',
                    message: '请选择一条数据进行修改',
                    position: 'bottom-right'
                });
                return false;
            }
            setTimeout(() => {
                this.$refs.FileModification.modify_display_notice();
            }, 10);

            this.dialogVisibleEdit = true;
        },
        //审核弹出框
        auditEngineering() {
            if (this.multipleSelection.length != 1 || this.multipleSelection.length > 1) {
                this.$notify.error({
                    title: '错误',
                    message: '请选择一条数据进行审核',
                    position: 'bottom-right'
                });
                return false;
            }
            setTimeout(() => {
                this.$refs.auditIndex.modify_display_notice();
            }, 10);
            // this.getTableData()
            this.dialogVisibleAudit = true;
        },
        // 添加保存
        saveAdd() {
            // 调用子路由方法
            this.$refs.FileAdd.addSave();
        },
        closeAdd(val) {
            this.dialogVisibleAdd = val;
            this.getTableData();
        },
        // 修改保存
        saveEdit() {
            // 调用子路由方法
            this.$refs.FileModification.editSave();
        },
        closeEdit(val) {
            console.log('==修改==');
            this.dialogVisibleEdit = val;
            this.getTableData();
        },
        closeExamine(val) {
            console.log('--博阳=-');
            this.dialogVisibleAudit = val;
            this.getTableData();
        },
        // 审核保存
        saveAudit() {
            // 调用子路由方法
            // this.auditStatus = 1,
            this.$refs.auditIndex.to_examine_noticeId(1);

            // this.getTableData()
            // this.dialogVisibleAudit=false
        },
        unsaveAudit() {
            // 调用子路由方法

            this.$refs.auditIndex.to_examine_noticeId(3);
        },
        closeAudit(val) {
            this.dialogVisibleAudit = val;
            // this.query();
        },
        //点击发布
        releaseEngineering() {
            if (this.multipleSelection.length == 0 || this.multipleSelection.length > 1) {
                this.$notify.error({
                    title: '错误',
                    message: '请选择一条数据进行发布',
                    position: 'bottom-right'
                });
                return false;
            }
            var roleId = {
                id: this.multipleSelection[0].id,
                type: 2
            };

            to_examine_NoticeId(roleId)
                .then(response => {
                    console.log('我是修改需要的回显');
                    if (response.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '发布成功!'
                        });
                        this.getTableData();
                    } else {
                        this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {
                    // console.log("我是点击了取消")
                });

            // setTimeout(()=>{
            // 	 this.$refs.auditIndex.to_examine_noticeId(2);
            // },20)
            // this.$refs.auditIndex.to_examine_noticeId(2);
            // var params_one = {
            //   id: this.multipleSelection[0].id
            // }
            // console.log(params_one)
            // checkNoticeStatus(params_one)
            //     .then(response => {
            //       this.auditStatus=2
            //       console.log( '发布'+response)
            //         this.$message({
            //             type: 'success',
            //             message: '发布成功!'
            //         });
            //         // this.noticeList();
            //     })
            //     .catch(() => {
            //         // this.loading = false
            //         // this.$message.error(response.data.msg)
            //     });
        },
        //勾选复选框
        handleSelectionChange(val) {
            console.log('===---', val);
            this.multipleSelection = val;
        },
        //删除
        deleteEngineering() {
            if (this.multipleSelection.length == 0 || this.multipleSelection.length > 1) {
                this.$notify.error({
                    title: '错误',
                    message: '请选择一条数据进行删除',
                    position: 'bottom-right'
                });
                return false;
            }
            var params_one = {
                id: this.multipleSelection[0].id
            };
            deleteNotice(params_one)
                .then(response => {
                    /*****对数据进行加载******/

                    /****对数据进行加载******/
                    if (response.data.code == 200) {
                        this.getTableData();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        // this.$refs.FileModification.roleDeptTreeselect1s
                    } else {
                        this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {
                    // this.loading = false
                    // this.$message.error(response.data.msg)
                });
            this.getTableData();
        },
        handleCurrentChange(val) {
            this.pageNum = val;
            this.getTableData();
        },
        onEnterSearch(e, searchVal) {
            /***********调用模糊查询接口***************/

            var new_add_params = {
                pageNum: 1,
                pageSize: 10,
                title: this.search //公告标题搜索
                // auditStatus: this.search
            };
            console.log('ss' + this.search);
            noticeSearch(new_add_params)
                .then(response => {
                    if (response.data.code == 200) {
                        this.tableData = response.data.data.list;
                        this.$message({
                            message: '查询成功',
                            type: 'success'
                        });
                    } else {
                        this.$message.error(response.data.msg);
                    }
                })
                .catch(() => {});
            /*********调用模糊查询的接口**************/
        }
        /**********模糊查询搜索功能****************/
    },
    mounted: function() {
        this.getTableData();
    }
};
</script>
<style scoped>
</style>